package com.itheima.app.manager;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.Notification;
import com.itheima.domain.db.Question;
import com.itheima.service.db.BlackListService;
import com.itheima.service.db.NotificationService;
import com.itheima.service.db.QuestionService;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.SettingVo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author Guanxing
 * @date 2021/9/20
 */
@Component
public class SettingsManager {

    @Reference
    private QuestionService questionService;

    @Reference
    private NotificationService notificationService;

    @Reference
    private BlackListService blackListService;

    //查询用户的通知信息
    public SettingVo findSettings() {
        //获得用户的id
        Long id = UserHolder.getId();

        //查阅用户的问题
        Question question = questionService.findByUid(id);
        //查询用户的通知
        Notification notification = notificationService.findByUid(id);

        //封装vo
        SettingVo settingVo = new SettingVo();
        settingVo.setId(id);
        settingVo.setPhone(UserHolder.get().getPhone());
        if (question == null) {
            settingVo.setStrangerQuestion("你喜欢高山还是流水?");//如果数据库没有 给一个默认问题
        } else {
            settingVo.setStrangerQuestion(question.getStrangerQuestion());
        }
        if (notification == null) {
            //如果没有设置任何通知,就全设置为关闭
            settingVo.setGonggaoNotification(false);
            settingVo.setLikeNotification(false);
            settingVo.setPinglunNotification(false);
        } else {
            settingVo.setGonggaoNotification(notification.getGonggaoNotification());
            settingVo.setLikeNotification(notification.getLikeNotification());
            settingVo.setPinglunNotification(notification.getPinglunNotification());
        }

        return settingVo;
    }

    public void saveOrUpdateQuestion(String content) {
        //获取用户id
        Long id = UserHolder.getId();
        //根据id查询用
        Question question = questionService.findByUid(id);
        if (question == null) {
            question = new Question();
            question.setUserId(id);
            question.setStrangerQuestion(content);
            questionService.save(question);
        } else {
            //执行修改
            question.setStrangerQuestion(content);
            questionService.update(question);
        }
    }

    //保存或修改通知设置
    public void saveOrUpdateNotifications(Notification notification) {
        //获得用户的id
        Long id = UserHolder.getId();
        //根据id查询通知对象
        Notification notification1 = notificationService.findByUid(id);
        if (notification == null) {
            notification = new Notification();
            notification.setId(id);
            notificationService.save(notification);
        } else {
            notification.setGonggaoNotification( notification.getGonggaoNotification() );
            notification.setLikeNotification( notification.getLikeNotification() );
            notification.setPinglunNotification( notification.getPinglunNotification() );
            notificationService.update(notification);
        }
    }

    //查询黑名单数据
    public PageBeanVo findBlackList(Integer page, Integer pageSize) {
        PageBeanVo pageBeanVo = blackListService.findBlackUserByUserId(UserHolder.getId(), page, pageSize);
        return pageBeanVo;
    }

    //删除黑名单用户信息
    public void deleteBlackUser(Long blackUserId) {
        Long loginUserId = UserHolder.getId();
        blackListService.deleteBlackUser(loginUserId,blackUserId);
    }
}
